import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:async';

class RxDartDemo extends StatelessWidget {
  const RxDartDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('rxDart'),
      ),
      body: RxDartHomeDemo(),
    );
  }
}

class RxDartHomeDemo extends StatefulWidget {
  RxDartHomeDemo({Key key}) : super(key: key);

  _RxDartHomeDemoState createState() => _RxDartHomeDemoState();
}

class _RxDartHomeDemoState extends State<RxDartHomeDemo> {
  PublishSubject<String> _publishSubject;

  @override
  void initState() {
    super.initState();
    _publishSubject = PublishSubject<String>();

    _publishSubject
        .map((item) => 'item:$item')
        .where((item) => item.length > 13)
        .debounceTime(Duration(seconds: 1))
        .listen((data) => print('$data'));

    // Observable<String> observable =
    // Observable(Stream.fromIterable(['elements', 'aaa', 'bbb']));
    // Observable.fromFuture(Future.value('aaaa'));
    // Observable.fromIterable(['aa', 'bb']);
    // Observable.just('ss');
    // Observable.periodic(Duration(seconds: 3), (count) => count.toString());

    // observable.listen(print);
    // PublishSubject<String> _publicSubject = PublishSubject<String>();
    // _publicSubject.listen((data) => print('listen1:${data}'));
    // _publicSubject.listen((data) => print('listen2:${data.toLowerCase()}'));
    // _publicSubject.listen((data) => print('listen1:${data.toUpperCase()}'));

    // _publicSubject.add('eventAdafad');

    // _publicSubject.close();

    // BehaviorSubject<String> _behaviorSubject = BehaviorSubject<String>();
    // _behaviorSubject.add('event1');
    // _behaviorSubject.add('23');
    // _behaviorSubject.listen((data) => print('listen1: ${data}'));
    // _behaviorSubject.listen((data) => print('listen2: ${data}'));
    // _behaviorSubject.close();

    // ReplaySubject<String> replaySubject = ReplaySubject<String>(maxSize: 2);

    // replaySubject.add('event1');
    // replaySubject.add('event2');
    // replaySubject.listen((data) => print('listen1:$data'));
    // replaySubject.listen((data) => print('listen2:$data'));
    // replaySubject.listen((data) => print('listen3:$data'));
    // replaySubject.close();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _publishSubject.close();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        onChanged: (value) {
          _publishSubject.add('change: $value');
        },
        onTap: () {
          _publishSubject.add('start tap');
        },
        onSubmitted: (value) {
          _publishSubject.add('submit:$value');
        },
        decoration: InputDecoration(labelText: '请输入'),
      ),
    );
  }
}
